﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using Progresoft.Scheduler.RemoteInterface.Api;
using Progresoft.Scheduler.RemoteInterface;

/// <summary>
/// Поисковик по таскэвентам
/// </summary>
public class TaskEventSearcher
{
    SchedulerServer server;
    string systemName;
    
    /// <summary>
	/// Список задач
	/// </summary>
    public List<ScheduledTaskEvent> ListScheduledTaskEvents(string systemName,
        string taskKey, int pageSize, int startIndex)
    {
        server = LoadServer(systemName);
        return server.ListScheduledTaskEvents(systemName, taskKey, null, null, null, null, null, pageSize, startIndex);
    }

    /// <summary>
    /// Список задач
    /// </summary>
	public int ListScheduledTaskEventsCount(string systemName, string taskKey)
    {
        server = LoadServer(systemName);
        return server.ListScheduledTaskEventsCount(systemName, taskKey, null, null, null, null);
    }

    protected SchedulerServer LoadServer(string systemName)
    {
        SchedulerServer serverToReturn;
        if (string.IsNullOrEmpty(this.systemName))
        {
            string url = ConnectorManager.GetServerUrl(systemName);
            this.systemName = systemName;
            serverToReturn = ConnectorManager.GetConnector().GetServer(url);
        }
        else
        {
            // защита от недопонимания
            if (this.systemName != systemName)
                throw new ArgumentException(string.Format("Searcher вызван для другой системы. Ожидается: {0}, передано: {1}",
                    this.systemName, systemName));
            serverToReturn = server;
        }
        return serverToReturn;
    }
}
